<?php
defined('PHPFOX') or exit('NO DICE!');

class Hoidap_Component_Controller_Postcauhoi extends Phpfox_Component
{
	
	public function process()
	{			
		$id_cauhoi = $this->request()->getInt('req3');
		if(isset($id_cauhoi) && $id_cauhoi > 0)
		{
			$this->template()->setTitle('Sửa câu hỏi');	
			
			$cauhoi = Phpfox::getService('hoidap.process')->selectCauHoiById($id_cauhoi); 
			
			$user_id = $cauhoi['user_id'];
			if($user_id != Phpfox::getUserId())
			{				
				Phpfox::permalink('hoidap', $cauhoi['cauhoi_id'], $cauhoi['title'], true, 'Bạn không có quyền');
				return;
			}
			$this->template()->assign('aForms', $cauhoi); 
		
			$category = Phpfox::getService('hoidap.category')->get('30'); 
			$this->template()->assign('category', $category);  
			
			$category_parent = Phpfox::getService('hoidap.category')->get('1',' category_id='.$cauhoi['category_id']); 
			$id_parent = $category_parent[0]['category_parent'];
			$this->template()->assign('category_parent', $id_parent);  
						
			$ListCategory = Phpfox::getService('hoidap.category')->selectListCategoryById($id_parent); 
			$this->template()->assign('listcategory', $ListCategory);  
			
		}else{
			// them moi
			$this->template()->setTitle('Đăng câu hỏi');
			$category = Phpfox::getService('hoidap.category')->get('10'); 
			$this->template()->assign('category', $category);  
					
		}	
			if($aVals = $this->request()->getArray('val'))
			{
					
				$aValidation = array(
					'title' => array(
						'def' => 'required',
						'title' => Phpfox::getPhrase('hoidap.fill_title_for_cauhoi')
					),
					'text' => array(
						'def' => 'required',
						'title' => Phpfox::getPhrase('hoidap.add_content_to_cauhoi')
					),
					'category_id' => array(
						'def' => 'required',
						'title' =>  Phpfox::getPhrase('hoidap.select_category_cauhoi')
					)
				);
				
				$oValid = Phpfox::getLib('validator')->set(array(
						'sFormName' => 'core_js_blog_form', 
						'aParams' => $aValidation
					)
				);			
			
			
				if ($oValid->isValid($aVals))
				{	
				
					$aVals['title'] = $aVals['title'];
					$aVals['text'] = $aVals['text'];
					$aVals['category_id'] = $aVals['category_id'];
					$aVals['cat_parent'] = $aVals['cat_parent'];
						
					if ($aVals['cauhoi_id'])
					{
					// Sua cau hoi
						Phpfox::getService('hoidap.process')->update($aVals, $aVals['cauhoi_id']);						
						$sMessage = "Sửa thành công";						
						Phpfox::permalink('hoidap', $aVals['cauhoi_id'], $aVals['title'], true, $sMessage);
					}else
					{
					// them cau hoi				
						$idinsert = Phpfox::getService('hoidap.process')->add($aVals);						
						$sMessage = "Thêm thành công";						
						Phpfox::permalink('hoidap', $idinsert, $aVals['title'], true, $sMessage);
					
					}					
				}
			}
		
			$this->template()
				->setEditor(array('wysiwyg' => Phpfox::getUserParam('blog.can_use_editor_on_blog')))
				->setHeader('cache', array(
					'jquery/plugin/jquery.highlightFade.js' => 'static_script',
					'switch_legend.js' => 'static_script',
					'switch_menu.js' => 'static_script',
					'quick_edit.js' => 'static_script',
					'pager.css' => 'style_css'
				)
			);	
			
			if (Phpfox::isModule('attachment') && Phpfox::getUserParam('attachment.can_attach_on_blog'))
			{
				$this->setParam('attachment_share', array(
						'type' => 'blog',
						'id' => 'core_js_blog_form',
						'edit_id' => ($bIsEdit ? $iEditId : 0)
					)
				);
			}		
	}
}

?>
